var rendererMD = new marked.Renderer();
marked.setOptions({
  renderer: rendererMD,
  gfm: true,
  tables: true,
  breaks: false,
  pedantic: false,
  sanitize: false,
  smartLists: true,
  smartypants: false
});
//home-loading-markdown
document.addEventListener('DOMContentLoaded', function () {
  const markdownFiles = ['../markdown/card1.md', '../markdown/card2.md', '../markdown/card3.md', '../markdown/card4.md'];
  const previews = [document.getElementById('card1'), document.getElementById('card2'), document.getElementById('card3'), document.getElementById('card4')];

  function loadMarkdown(index) {
    const filePath = markdownFiles[index];
    fetch(filePath)
      .then(response => {
        if (!response.ok) throw new Error('文件加载失败');
        return response.text();
      })
      .then(markdownText => {
        const html = marked.parse(markdownText);
        previews[index].innerHTML = html;
      })
      .catch(error => {
        console.error('加载 Markdown 文件出错:', error);
        previews[index].innerHTML = `<p style="color: red;">加载失败</p>`;
      });
    // const markdownText = "# Hello, Marked!";
    // const html = marked.parse(markdownText); // v5+ 写法
    // document.getElementById("card1").innerHTML = html;
  }


  markdownFiles.forEach((_, index) => loadMarkdown(index));
});


//loading-learning-task-markdown
document.addEventListener('DOMContentLoaded', function () {
  const markdownFiles = ['../markdown/task1.md',
     '../markdown/task2.md', 
     '../markdown/task3.md', 
     '../markdown/task4.md',
     '../markdown/task5.md',
     '../markdown/task6.md'];
  const previews = [document.getElementById('learning_task_1'),
     document.getElementById('learning_task_2'), 
     document.getElementById('learning_task_3'),
     document.getElementById('learning_task_4'),
     document.getElementById('learning_task_5'),
     document.getElementById('learning_task_6'),
     ];

  function loadMarkdown(index) {
    const filePath = markdownFiles[index];
    fetch(filePath)
      .then(response => {
        if (!response.ok) throw new Error('文件加载失败');
        return response.text();
      })
      .then(markdownText => {
        const html = marked.parse(markdownText);
        previews[index].innerHTML = previews[index].innerHTML = `
          <div class="markdown-card">
            <div class="card_content">
              ${html}
            </div>
          </div>
        `;
      })
      .catch(error => {
        console.error('加载 Markdown 文件出错:', error);
        previews[index].innerHTML = previews[index].innerHTML = `
          <div class="markdown-card error">
            <div class="card_content">
              <p style="color: red;">加载失败</p>
            </div>
          </div>
        `;
      });
    // const markdownText = "# Hello, Marked!";
    // const html = marked.parse(markdownText); // v5+ 写法
    // document.getElementById("card1").innerHTML = html;
  }


  markdownFiles.forEach((_, index) => loadMarkdown(index));
});
